﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class BigCustomerController : BaseController<customer>
    {
        //
        // GET: /BigCustomer/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public override BLL.BaseBll<customer> bll
        {
            get
            {
                return new BigCustomerBll();
            }
        }

        public class ViewCustomer
        {
            public long id { get; set; }
            public string emplyName { get; set; }
            public string abbreviation { get; set; }
            public DateTime? actualdate { get; set; }
            public string address { get; set; }
            public string contacts { get; set; }
            public string contactsphone { get; set; }
            public string contractstatus { get; set; }
            public DateTime? createtime { get; set; }
            public Nullable<int> equipment { get; set; }
            public string industry { get; set; }
            public string label { get; set; }
            public string launchstatus { get; set; }
            public string level { get; set; }
            public string name { get; set; }
            public DateTime? plandate { get; set; }
            public string remarks { get; set; }
            public string status { get; set; }
            public DateTime? updatetime { get; set; }
        }



        CustomerBDRelationBll customerBdbll = new CustomerBDRelationBll();

        EmplyInfoBll emplyBll = new EmplyInfoBll();

        //model, x =>true, true, x => x.id, rows, page, out count
        // id name abbreviation  contacts  contactsphone industry  address  level  equipment
        // plandate  actualdate contractstatus  launchstatus  createtime remarks
        public ActionResult Get2(int page, int rows, customer model, string companyName)
        {            
            var listCustomer = new List<ViewCustomer>();
            //List<customer> list = new List<customer>();
            if (!string.IsNullOrEmpty(companyName))
            {
                var list = bll.Search().Where(x => x.name.Contains(companyName)).Select(x => new
                 {
                     x.id,
                     x.name,
                     x.abbreviation,
                     x.label,
                     level = x.level == 1 ? "A级" : x.level == 2 ? "B级" : "C级",
                     x.address,
                     x.contactsphone,
                     x.contacts,
                     x.plandate,
                     x.actualdate,
                     x.equipment,
                     x.industry,
                     contractstatus = x.contractstatus == 1 ? "签约客户" : x.contractstatus == 2 ? "意向客户" : "无意向",
                     x.remarks,
                     launchstatus = x.launchstatus == 1 ? "已投放" : "未投放",
                     x.createtime,
                 });
                foreach (var item in list)
                {
                    ViewCustomer viewmodel = new ViewCustomer();
                    viewmodel.id = item.id;
                    viewmodel.name = item.name;
                    viewmodel.abbreviation = item.abbreviation;
                    viewmodel.label = item.label;
                    viewmodel.level = item.level;
                    viewmodel.address = item.address;
                    viewmodel.contactsphone = item.contactsphone;
                    viewmodel.contacts = item.contacts;
                    viewmodel.plandate = item.plandate;
                    viewmodel.actualdate = item.actualdate;
                    viewmodel.equipment = item.equipment;
                    viewmodel.industry = item.industry;
                    viewmodel.contractstatus = item.contractstatus;
                    viewmodel.remarks = item.remarks;
                    viewmodel.launchstatus = item.launchstatus;
                    viewmodel.createtime = item.createtime;
                    int emplyId = customerBdbll.Search(x => x.customerId == item.id).Select(x => new { x.emplyId }).FirstOrDefault().emplyId;
                    string emplyName = emplyBll.Search(x => x.emplyID == emplyId).Select(x => new { x.emplyName }).FirstOrDefault().emplyName;
                    viewmodel.emplyName = emplyName;
                    listCustomer.Add(viewmodel);
                }
            }
            else
            {
                var list = bll.Search().Select(x => new
                {
                    x.id,
                    x.name,
                    x.abbreviation,
                    x.label,
                    level = x.level == 1 ? "A级" : x.level == 2 ? "B级" : "C级",
                    x.address,
                    x.contactsphone,
                    x.contacts,
                    x.plandate,
                    x.actualdate,
                    x.equipment,
                    x.industry,
                    contractstatus = x.contractstatus == 1 ? "签约客户" : x.contractstatus == 2 ? "意向客户" : "无意向",
                    x.remarks,
                    launchstatus = x.launchstatus == 1 ? "已投放" : "未投放",
                    x.createtime,
                });
                foreach (var item in list)
                {
                    ViewCustomer viewmodel = new ViewCustomer();
                    viewmodel.id = item.id;
                    viewmodel.name = item.name;
                    viewmodel.abbreviation = item.abbreviation;
                    viewmodel.label = item.label;
                    viewmodel.level = item.level;
                    viewmodel.address = item.address;
                    viewmodel.contactsphone = item.contactsphone;
                    viewmodel.contacts = item.contacts;
                    viewmodel.plandate = item.plandate;
                    viewmodel.actualdate = item.actualdate;
                    viewmodel.equipment = item.equipment;
                    viewmodel.industry = item.industry;
                    viewmodel.contractstatus = item.contractstatus;
                    viewmodel.remarks = item.remarks;
                    viewmodel.launchstatus = item.launchstatus;
                    viewmodel.createtime = item.createtime;
                    int emplyId = customerBdbll.Search(x => x.customerId == item.id).Select(x => new { x.emplyId }).FirstOrDefault().emplyId;
                    string emplyName = emplyBll.Search(x => x.emplyID == emplyId).Select(x => new { x.emplyName }).FirstOrDefault().emplyName;
                    viewmodel.emplyName = emplyName;
                    listCustomer.Add(viewmodel);
                }
            }
            var orderList = listCustomer.OrderByDescending(x => x.createtime).ToPagedList(page, rows).ToList();
            return Json(new { total = listCustomer.Count, rows = orderList });
        }

        public ActionResult GetOnlyOne(long id)
        {
            //https://www.cnblogs.com/menyiin/p/5363707.html  时间控件
            return Json(bll.Search(x => x.id == id).Select(x => new
            {
                x.id,
                launchstatus = x.launchstatus == 1 ? "已投放" : x.launchstatus == 0 ? "未投放" : "撤架",

            }).ToList()[0], JsonRequestBehavior.AllowGet);
        }

        public ActionResult Modify1(ViewCustomer model)
        {
            customer c = bll.Search(x => x.id == model.id).FirstOrDefault();
            string msg = "修改失败";
            string state = "false";
            c.launchstatus = Convert.ToInt32(model.launchstatus);
            c.updatetime = DateTime.Now;
            if (bll.Modify(c) > 0)
            {
                msg = "修改成功";   
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }
    }
}
